﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ylh.Enums;

namespace Ylh.Data
{
    public class QueryWhere
    {
        public String StrWhere { get; set; }
        public Dictionary<String, Object> _preWhere = new Dictionary<string, object>();
        public Dictionary<String, Object> PreWhere
        {
            get { return _preWhere; }
            set { _preWhere = value; }
        }
        private String PropertyName { get; set; }
        private String ParameterName { get; set; }
        private static decimal ParaCount = 0;
        public QueryWhere(EntityBase e)
        {
            if (ParaCount == decimal.MaxValue - 1) { ParaCount = 0; }
            e.PropertyNameGetting += new EventHandler<PropertyNameGettingEventArgs>(PropertyNameGetting);
        }

        protected void PropertyNameGetting(object sender, PropertyNameGettingEventArgs e)
        {
            PropertyName = e.PropertyName;
            ParameterName = "@" + PropertyName + ParaCount;
            ParaCount += 1;
        }

        public QueryWhere MosaicWhere(Enums.LogicType LogicType, String OperateType, String FildName, object O)
        {
            switch (OperateType)
            {
                case Enums.OperateType.等于:
                case Enums.OperateType.不等于:
                case Enums.OperateType.大于:
                case Enums.OperateType.大于等于:
                case Enums.OperateType.小于:
                case Enums.OperateType.小于等于:
                case Enums.OperateType.IS:
                    StrWhere += " " + LogicType + " " + PropertyName + " " + OperateType + " " + ParameterName;
                    break;
                default:
                    break;
            }
            CreateParameter(ParameterName, O);
            return this;
        }

        public void CreateParameter(string ParameterName, object O)
        {
            if (O == null)
            {
                throw new Exception("对比的值不能为空");
            }

            if (O.GetType().BaseType == typeof(Array))
            {
                throw new Exception("单独逻辑运算符不能使用数组作为值进行对比");
            }

            PreWhere.Add(ParameterName, O);
        }

        public QueryWhere AND(object FildName, String OperateType, object O)
        {
            return MosaicWhere(Enums.LogicType.AND, OperateType, PropertyName, O);
        }

        public QueryWhere OR(object FildName, String OperateType, object O)
        {
            return MosaicWhere(Enums.LogicType.OR, OperateType, PropertyName, O);
        }




    }
}


